﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace 自定义多选下拉框
{
    public class MultiSelectConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is IList selectedItems && parameter != null)
            {
                return selectedItems.Contains(parameter);
            }
            return false;
        }

        public object ConvertBack(
            object value,
            Type targetType,
            object parameter,
            CultureInfo culture
        )
        {
            if (value is bool isChecked && parameter != null && parameter is IList selectedItems)
            {
                if (isChecked)
                {
                    if (!selectedItems.Contains(parameter))
                        selectedItems.Add(parameter);
                }
                else
                {
                    if (selectedItems.Contains(parameter))
                        selectedItems.Remove(parameter);
                }
            }
            return null;
        }
    }
}
